package com.example.recordingpen_datacollection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.recordingpen_datacollection.Utils.String.StringUtils;
import com.example.recordingpen_datacollection.Utils.String.StringValidator;
import com.example.recordingpen_datacollection.Utils.Token.UniqueTokenUtil;
import com.example.recordingpen_datacollection.entity.Login;
import com.example.recordingpen_datacollection.entity.Query.UserDevQue;
import com.example.recordingpen_datacollection.entity.Query.UserListQue;
import com.example.recordingpen_datacollection.entity.UserMessage;
import com.example.recordingpen_datacollection.entity.Vo.*;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.UserMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.example.recordingpen_datacollection.config.constant.ConstantName.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2024-06-03
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/user-message")
public class UserMessageController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private UserMessageService userMessageService;

    @ApiOperation(value = "用户添加接口[admin]")
    @PostMapping("setUserMess")
    public RLIST setUserMess(@RequestBody UserMessVo userMessVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if(token.length() != 56){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);
        /*
        数据校验---
         */


        boolean flat= userMessageService.setUserMessage(userMessVo,value);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }

    }
    //USER_MASS_TOKEN62a8c0af-cb15-4a05-b1e2-4faa94e6b471
    @ApiOperation(value = "用户登录[user]")
    @PostMapping("loginUser")
    public RLIST loginUser(@RequestBody UserLoginVo userLoginVo, HttpServletRequest request){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("uaccount",userLoginVo.getUaccount());
        wrapper.eq("upassword",userLoginVo.getUpassword());
        UserMessage one = userMessageService.getOne(wrapper);
        if(one == null){
            return RLIST.error().message("信息不准确无法登录");
        }
        //生成token
        String token = USER_MESS_TOKEN+UniqueTokenUtil.generateToken();
        System.out.println(redisService  +" redisService");
        String value1 = (String) redisService.getValue(one.getUid().toString());
        if(value1 != null){
            redisService.delKeyData(value1);
        }
        redisService.setTokenValueDAYS(token,1L, one.getUid());
//        redisService.setTokenValueDAYS(one.getUid().toString(),1L,token);
        return RLIST.ok().data("token",token);
    }


    @ApiOperation(value = "用户修改密码 [admin - user]")
    @PostMapping("alterUser")
    public RLIST alterUser(@RequestBody AlterUserVo alterUserVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if(!(token.length() == 51 || token.length() == 56)){
            return RLIST.error().message("无权操作");
        }

        //全校验
        if(StringUtils.isEmpty(alterUserVo.getNewPassword())){
            return RLIST.error().message("新密码不允许为空");
        }


        Integer value = (Integer) redisService.getValue(token);
        if(token.length() == 51){
            //用户校验
            if(StringUtils.isEmpty(alterUserVo.getNewPassword())){
                return RLIST.error().message("新密码不允许为空");
            }

            UserMessage byId = userMessageService.getById(value);
            if(byId ==null){
                return RLIST.error().message("信息无效");
            }
            if(!alterUserVo.getPassword().equals(byId.getUpassword())){
                return RLIST.error().message("密码不匹配");
            }
        } else if(token.length() == 56) {
            //超级用户校验
            if(alterUserVo.getUid() == null || alterUserVo.getUid() <=0){
                return RLIST.error().message("超级管理修改,ID不允许为空");
            }
            value = alterUserVo.getUid();
        }

        boolean flat = userMessageService.alterUserPass(value, alterUserVo.getNewPassword());
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }


    @ApiOperation(value = "查询用户下的设备 [admin - user]")
    @PostMapping("getUser_dev")
    public RLIST getUserDev(@RequestBody UserDevQue userDevQue, HttpServletRequest request){
        String token = request.getHeader("token");
        if( !(token.length() == 51 || token.length() == 56) ){
            return RLIST.error().message("无权操作");
        }
        //"USER_MESS_TOKENcaef19e6-b721-49bc-b012-fe3c98a6a708"
        Integer value = (Integer) redisService.getValue(token);
        if(token.length() == 51){
            UserMessage byId = userMessageService.getById(value);
            if(byId ==null){
                return RLIST.error().message("信息无效");
            }
        }
        if ((userDevQue.getPageNo() == null || userDevQue.getPageNo() <=0) ){
            userDevQue.setPageNo(1);
        }
        if ((userDevQue.getPageSize() == null || userDevQue.getPageSize() <=0) ){
            userDevQue.setPageSize(10);
        }
        if(token.length() == 56){
            value = 0;
        }
        RLIST userDev = userMessageService.getUserDev(userDevQue, value);
        return userDev;
    }

    @ApiOperation(value = "修改用户最大时间 [admin]")
    @PostMapping("setUserMaxTime")
    public RLIST setUserMaxTime(@RequestBody UserMaxTimeVo userMaxTimeVo , HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56){
            return RLIST.error().message("无权操作");
        }
        UserMessage byId = userMessageService.getById(userMaxTimeVo.getUserId());
        if(byId == null){
            return RLIST.error().message("信息不存在");
        }
        boolean flat = userMessageService.setUserMaxTime(userMaxTimeVo);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }
    //UserLimitQuantity
    @ApiOperation(value = "修改用户配额")
    @PostMapping("setUserLimitQuantity [admin]")
    public RLIST setUserLimitQuantity(@RequestBody UserLimitQuantityVo userLimitQuantityVo , HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56){
            return RLIST.error().message("无权操作");
        }
        UserMessage byId = userMessageService.getById(userLimitQuantityVo.getUserId());
        if(byId == null){
            return RLIST.error().message("信息不存在");
        }
        boolean flat = userMessageService.setUserLimitQuantity(userLimitQuantityVo,byId);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }

    @ApiOperation(value = "修改用户最大时间和配额 [admin]")
    @PostMapping("setTimeLimit")
    public RLIST setTimeLimit(@RequestBody UserTimeLimitVo userTimeLimitVo , HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56){
            return RLIST.error().message("无权操作");
        }
        UserMessage byId = userMessageService.getById(userTimeLimitVo.getUserId());
        if(byId == null){
            return RLIST.error().message("信息不存在");
        }
        boolean flat = userMessageService.setUserTimeLimit(userTimeLimitVo,byId);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }



    @ApiOperation(value = "查询全部用户 [admin]")
    @PostMapping("getUserList")
    public RLIST getUserList(@RequestBody UserListQue userListQue , HttpServletRequest request){
        if ((userListQue.getPageNo() == null || userListQue.getPageNo() <=0) ){
            userListQue.setPageNo(1);
        }
        if ((userListQue.getPageSize() == null || userListQue.getPageSize() <=0) ){
            userListQue.setPageSize(10);
        }
        String token = request.getHeader("token");
        if( token.length() != 56){
            return RLIST.error().message("无权操作");
        }

        RLIST rlist = userMessageService.getUserListPage(userListQue);
        return rlist;
    }


}

